﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace Kokomo.PeInspector.Reflection
{
	/// <summary>
	/// Represents a resource within an assembly.
	/// </summary>
	/// <seealso cref="AssemblyManifestResource"/>
	/// <seealso cref="AssemblyPackResource"/>
	public abstract class AssemblyResource
	{
		/// <summary>
		/// Gets the name of the resource.
		/// </summary>
		public string Name { get; private set; }
		/// <summary>
		/// Gets the assembly containing the resource.
		/// </summary>
		public Assembly Assembly { get; private set; }

		/// <summary>
		/// Initializes a new instance of <see cref="AssemblyResource"/>.
		/// </summary>
		/// <param name="name">The name of the resource.</param>
		/// <param name="assembly">The assembly containing the assembly.</param>
		public AssemblyResource(string name, Assembly assembly)
		{
			if (name == null) throw new ArgumentNullException("name");
			if (assembly == null) throw new ArgumentNullException("assembly");

			this.Name = name;
			this.Assembly = assembly;
		}

		/// <summary>
		/// Gets the data constituting the resource.
		/// </summary>
		/// <returns>A byte array containing the resource data.</returns>
		/// <remarks>
		/// This implementation calls <see cref="GetResourceStream"/> and
		/// then reads the stream into an array.
		/// </remarks>
		public virtual byte[] GetResourceData()
		{
			Stream stream = this.GetResourceStream();
			if (stream == null) return new byte[0];

			byte[] data = new byte[stream.Length];
			stream.Read(data, 0, data.Length);

			return data;
		}

		/// <summary>
		/// Gets the stream containing the resource data.
		/// </summary>
		/// <returns></returns>
		public abstract Stream GetResourceStream();
		/// <summary>
		/// Determines the size of the resource.
		/// </summary>
		/// <returns>The size of the resource, in bytes.</returns>
		public abstract int DetermineSize();
	}
}
